<?php

declare(strict_types=1);

namespace App\Robot\Crontab;

use App\Common\Robot\Robot;
use App\CustHub\Constants\RobotLoginStatusConstants;
use App\CustHub\Model\ChRobot;
use Hyperf\Coroutine\Parallel;

/**
 * 刷新CDN
 */
class CdnRefresherCrontab
{
    public function execute()
    {
        ChRobot::query()
            ->where('login_status',RobotLoginStatusConstants::ONLINE)
            ->chunk(100, function ($robotList){
                $parallel = new Parallel(10);
                foreach ($robotList as $robot){
                    $parallel->add(function () use ($robot) {
                        $apiService = make(Robot::class, [$robot->server_id, $robot->uuid])->gateway();
                        return $apiService->cdnRefresher();
                    });
                }
                $parallel->wait();
                \Hyperf\Coroutine\Coroutine::sleep(rand(1, 3));
            });
    }
}